import {
  ADD_BREAD,
  DELETE_BREAD,
  HIDE_HEADER
} from '../actions/Breadcrumb'
import { createBrowserHistory} from 'history'
const history = createBrowserHistory();
// const 
const reduxState = {
  breadListData: [{
    title: "主页",
    icon: "HomeOutlined",
    path: "/fileUpload"
  }],
  headerShow: true
}
const breadList = (state: any = reduxState, action:any): object => {
  switch (action.type) {
    case HIDE_HEADER:
      return {
        headerShow: false
      }
    case DELETE_BREAD:
      return { payload: action.payload };
    case ADD_BREAD:
      let payload = state.breadListData.some((res:any) => {
        return res.path === action.info.path
      })
      if(!payload){
        let breadData = null
        state.breadListData.forEach((res:any) => {
          if(res.path !== action.info.path){
            breadData = action.info
          }else{
            breadData = null
          }
        })
        if(breadData){
          let breadListData = [...state.breadListData]
          breadListData.push(breadData)
          return {
            breadListData:breadListData,
            headerShow: state.headerShow
          }
        }
      }else{
        let breadList = Object.create(state.breadListData)
        // 切割 LIST 数组进行动态展示
        let index = breadList.findIndex((info:any)=>{
          return info.path === action.info.path
        })
        // history.push(action.info.path)
        // splice 返回切割的元素数组  原数组改为剩余数据数组
        // slice 返回切割的元素数组   原数组不变
        breadList = breadList.splice(0,index+1)
        return {
          breadListData:breadList,
          headerShow: state.headerShow
        }
      };
    default : return state
  }
}
export default breadList